﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AusProject
{
    public partial class FormDropListValues : Form
    {
        private List<string> listValue;

        public List<string> ListValue
        {
            get
            {
                return listValue;
            }
            set
            {
                listValue = value;

                lstValues.Items.Clear();

                if (listValue != null)
                {
                    chkAllowNull.Checked = (listValue.Count > 0) && string.IsNullOrEmpty(listValue[0]);

                    foreach (string item in listValue)
                    {
                        if (!string.IsNullOrEmpty(item))
                        {
                            lstValues.Items.Add(item);
                        }
                    }

                    if (lstValues.Items.Count > 0)
                    {
                        lstValues.SelectedIndex = 0;
                        btnDel.Enabled = true;
                    }
                    else
                    {
                        btnDel.Enabled = false;
                        btnUp.Enabled = false;
                        btnDown.Enabled = false;
                    }
                }
            }
        }

        private bool hasAccepted = false;

        public bool HasAccepted
        {
            get { return hasAccepted; }
            set { hasAccepted = value; }
        }

        public FormDropListValues()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            txtNewValue.Text = txtNewValue.Text.Trim();
            if (string.IsNullOrEmpty(txtNewValue.Text))
            {
                return;
            }

            bool flag = true;
            foreach (string item in lstValues.Items)
            {
                if (item.ToUpper().CompareTo(txtNewValue.Text.ToUpper()) == 0)
                {
                    flag = false;
                    break;
                }
            }

            if (flag)
            {
                lstValues.Items.Add(txtNewValue.Text);
                lstValues.SelectedIndex = lstValues.Items.Count - 1;
                btnDel.Enabled = true;
            }
            else
            {
                MessageBox.Show("Cannot add duplicated value", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            int idx = lstValues.SelectedIndex;
            string item = (string)lstValues.Items[idx];
            
            lstValues.Items.RemoveAt(idx);
            lstValues.Items.Insert(idx - 1, item);
            lstValues.SelectedIndex = idx - 1;
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            int idx = lstValues.SelectedIndex;
            string item = (string)lstValues.Items[idx];

            lstValues.Items.RemoveAt(idx);
            lstValues.Items.Insert(idx + 1, item);
            lstValues.SelectedIndex = idx + 1;
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            int idx = lstValues.SelectedIndex;

            if (idx >= 0)
            {
                lstValues.Items.RemoveAt(idx);
            }

            if (lstValues.Items.Count > 0)
            {
                lstValues.SelectedIndex = idx < lstValues.Items.Count ? idx : idx - 1;
            }
            else
            {
                btnDel.Enabled = false;
                btnUp.Enabled = false;
                btnDown.Enabled = false;
            }
        }

        private void lstValues_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnUp.Enabled = lstValues.SelectedIndex > 0;
            btnDown.Enabled = lstValues.SelectedIndex < lstValues.Items.Count - 1;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            listValue.Clear();
            if (chkAllowNull.Checked)
            {
                listValue.Add("");  // User will choose this item as "null" value
            }
            foreach (string item in lstValues.Items)
            {
                listValue.Add(item);
            }
            hasAccepted = true;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            hasAccepted = false;
            this.Close();
        }

        private void txtNewValue_Enter(object sender, EventArgs e)
        {
            txtNewValue.SelectAll();
        }

        private void FormValueListEditor_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (this.ActiveControl == txtNewValue)
                {
                    btnAdd_Click(null, null);
                    e.Handled = true;
                    txtNewValue.SelectAll();
                }
            }
            else if (e.KeyCode == Keys.Delete)
            {
                if ((this.ActiveControl == lstValues) && (btnDel.Enabled))
                {
                    btnDel_Click(null, null);
                    e.Handled = true;
                }
            }

        }
    }
}
